package com.irate.activity;

import org.apache.http.Header;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.irate.dao.UserDAO;
import com.irate.http.AsyncHttpResponseHandler;
import com.irate.provider.ContentManager;
import com.irate.provider.SessionLogin;

public class WelcomeActivity extends Activity {

	protected TextView sign_in;
	protected TextView sign_up_fb;
	protected TextView sign_up;

	protected UserDAO userDAO;
	protected ProgressDialog dialog;

	protected SessionLogin session;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		userDAO = new UserDAO();
		session = new SessionLogin(this);

		if (session.getLoginState()) {
			String[] loginData = session.getLoginSession();
			if (!loginData[0].equals("") && !loginData[1].equals("")) {
				userDAO.login(loginData[0], loginData[1], handler, this, dialog);
			}
		} else {

			setContentView(R.layout.activity_welcome);

			init();
			execute();
		}
	}

	private void init() {

		sign_in = (TextView) findViewById(R.id.login_sign_in);
		sign_up_fb = (TextView) findViewById(R.id.login_sign_up_fb_button);
		sign_up = (TextView) findViewById(R.id.login_sign_up);
	}

	private void execute() {

		sign_in.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(WelcomeActivity.this,
						LoginActivity.class));
				finish();
			}
		});

		sign_up.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(WelcomeActivity.this,
						CreateAccountActivity.class), 1);
				finish();
			}
		});

		sign_up_fb.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
				if (con == null || con.getActiveNetworkInfo() == null
						|| !con.getActiveNetworkInfo().isConnected()) {

					Toast.makeText(v.getContext(),
							getResources().getString(R.string.msg_no_internet),
							Toast.LENGTH_SHORT).show();
				} else {

					Intent share = new Intent(WelcomeActivity.this,
							ShareActivity.class);
					share.putExtra("message", "login");

					startActivity(share);
					finish();

				}
			}
		});
	}

	AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] headers, byte[] response) {
			System.out.println("Success register");

			userDAO.parsedata(new String(response));

			if (ContentManager.getInstance().isLogin()) {

				session.setLoginState(true);

				startActivity(new Intent(WelcomeActivity.this,
						MainActivity.class));
				finish();
			} else {
				Toast.makeText(WelcomeActivity.this,
						getResources().getString(R.string.login_error),
						Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		public void onStart() {
			System.out.println("Start");
		}

		@Override
		public void onFinish() {
			if (dialog != null)
				dialog.dismiss();
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] errorResponse, Throwable e) {
			System.out.println("Fail Register");
			Toast.makeText(WelcomeActivity.this,
					getResources().getString(R.string.login_fail),
					Toast.LENGTH_SHORT).show();
		}
	};

}
